$(document).ready(function () {
    initPagination();
});

//条件查询开始
$("#distpicker1").distpicker({
    autoSelect: false
});

//条件查询结束
//表格头部字段筛选
$('#deviceDate_edit').on('focus', function () {
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,

        onpicked: function () {
            console.log($(this).val());
        }
    })
})
$('#deviceUpdate_edit').on('focus', function () {
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,

        onpicked: function () {
            console.log($(this).val());
        }
    })
})
//表格头部字段筛选
$('#deviceDate').on('focus', function () {
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,

        onpicked: function () {
            console.log($(this).val());
        }
    })
})
$('#deviceUpdate').on('focus', function () {
    WdatePicker({
        //minDate:'%y-%M-{%d+1}' ,

        onpicked: function () {
            console.log($(this).val());
        }
    })
})
var json = {};

var orderBy="";

$('.seek-btn').on('click',function(){
    initPagination();
});



//清空条件
$('.seek-clear').on('click', function () {
    $('#start-time').val('');
    $('#end-time').val('');
    $('#sbwybm').val('');
})

function clear() {

    $('sbwybm').val('');
}



//分页
function initPagination() {
    // 创建分页
    //获取总行数
    var totalcount = getCount();
    //按照下标进行查询
    $("#Pagination").pagination(totalcount, {
        num_edge_entries: 0, //边缘页数
        num_display_entries: 10, //主体页数
        callback: pageselectCallback, //回调函数
        items_per_page: 10, //每页显示10项
        prev_text: "上一页",
        next_text: "下一页",
        ellipse_text: "...",//省略的文本
        prev_show_always: true,//是否总显示“前一页”分页按钮
        next_show_always: true,//是否总显示“下一页”分页按钮，
    });
    $("#currentCount").text("总充值数量："+totalcount);
};
function pageselectCallback(page_index, jq) {
    query(page_index, jq);
}
//   表格部分结束
function query(page_index, jq) {
    //发送ajax;
    $.ajax(
        {
            type: "post",
            url: "/device/query",
            data: getJson(page_index, jq),
            dataType: "json",
            async: false,
            success: function (data) {
                var result = data.data;
                var tmpl1 = bt('list1', result);
                $("#tbody1").html(tmpl1);


            }, error: function () {
            alert("报错了");
        }
        }
    );
}

function getJson(page_index, jq) {
    var queryjson;
    queryjson = {
        deviceNum: $('#sbwybm')["0"].value,
        pageNum: page_index + 1,
        orderBy: orderBy
    }
    return queryjson;
}

function getCount() {
    var result = 0;
    var qjson=getJson(0, 0);
    $.ajax(
        {
            type: "post",
            url: "/device/count",
            data: qjson,
            dataType: "json",
            async: false,
            success: function (data) {
                result = data.data;
            }, error: function () {
            alert("报错了");
        }
        }
    );
    return result;
}



function getFormatDateStr(obj){
    return  getFormatDate(obj );
}


function showadd() {
    $('#add-device').cmsmodal('show');
}
function showedit(obj) {

    var tbody = $(obj).parents("tbody");
    var deviceId_edit = $(obj)["0"].dataset.value;
    var tr = $(obj).parents("tr");
  var deviceNum=  tr.find(".deviceNum").text();
  var deviceName=  tr.find(".deviceName").text();
  var deviceUrl=  tr.find(".deviceUrl").text();
  var devicePrice=  tr.find(".devicePrice").text();
  var deviceStatus=  tr.find(".deviceStatus").text();
  var deviceAdd=  tr.find(".deviceAdd").text();
  var deviceDate=  tr.find(".deviceDate").text();
  var deviceUpdate=  tr.find(".deviceUpdate").text();
  var remark=  tr.find(".remark").text();
    $("#deviceNum_edit").val(deviceNum);
    $("#deviceName_edit").val(deviceName);
    $("#devicePrice_edit").val(devicePrice);
    $("#deviceAdd_edit").val(deviceAdd);
    $("#deviceDate_edit").val(deviceDate);
    $("#deviceUpdate_edit").val(deviceUpdate);
    $("#deviceStatus_edit").val(deviceStatus);
    $("#deviceId_edit").val(deviceId_edit);
    $("#remark_edit").val(remark);
    $('#edit-device').cmsmodal('show');
}



function hideDialog() {
    $('#add-device').cmsmodal('hide');
    $('#edit-device').cmsmodal('hide');
    cleanAddDiv();
}
function cleanAddDiv() {
    $("#deviceNum").val('');
    $("#deviceName").val('');
    $("#devicePrice").val('');
    $("#deviceAdd").val('');
    $("#deviceDate").val('');
    $("#deviceUpdate").val('');
    $("#deviceStatus").val('');
    $("#remark").val('');

    $("#deviceNum_edit").val('');
    $("#deviceName_edit").val('');
    $("#devicePrice_edit").val('');
    $("#deviceAdd_edit").val('');
    $("#deviceDate_edit").val('');
    $("#deviceUpdate_edit").val('');
    $("#deviceStatus_edit").val('');
    $("#remark_edit").val('');
}

function add() {

    var deviceNum = $("#deviceNum").val();
    var deviceName = $("#deviceName").val();
    var devicePrice = $("#devicePrice").val();
    var deviceAdd = $("#deviceAdd").val();
    var deviceDate = $("#deviceDate").val();
    var deviceUpdate = $("#deviceUpdate").val();
    var deviceStatus = $("#deviceStatus").val();
    var remark = $("#remark").val();
    var json1 = {
        deviceNum: deviceNum,
        deviceName: deviceName,
        devicePrice: devicePrice,
        deviceDateStr: deviceDate,
        deviceUpdateStr: deviceUpdate,
        deviceAdd: deviceAdd,
        deviceStatus: deviceStatus,
        remark: remark
    };

    $.ajax({
        type: "post",
        url: "/device/add",
        data: json1,
        dataType: "json",
        success: function (data) {
            var result = data.data;

        }, error: function () {
            alert("报错了");
        }
    });

    cleanAddDiv();
    $('#add-device').cmsmodal('hide');
    initPagination();
}




function onurl(obj) {
    var tr = $(obj).parents("tr");
    var deviceNum=  tr.find(".deviceNum").text();
    location.href="/device/ewmurl?deviceNum="+deviceNum;
}


function edit() {

    var deviceId = $("#deviceId_edit").val();
    var deviceNum = $("#deviceNum_edit").val();
    var deviceName = $("#deviceName_edit").val();
    var devicePrice = $("#devicePrice_edit").val();
    var deviceAdd = $("#deviceAdd_edit").val();
    var deviceDate = $("#deviceDate_edit").val();
    var deviceUpdate = $("#deviceUpdate_edit").val();
    var deviceStatus = $("#deviceStatus_edit").val();
    var remark = $("#remark_edit").val();

    var json1 = {
        deviceId:deviceId,
        deviceNum: deviceNum,
        deviceName: deviceName,
        devicePrice: devicePrice,
        deviceAdd: deviceAdd,
        deviceDateStr: deviceDate,
        deviceUpdateStr: deviceUpdate,
        deviceStatus: deviceStatus,
        remark: remark
    };

    $.ajax({
        type: "post",
        url:  "/device/update",
        data: json1,
        dataType: "json",
        success: function (data) {
            var result = data.data;

        }, error: function () {
            alert("报错了");
        }
    });
    $('#edit-device').cmsmodal('hide');
    cleanAddDiv();
    initPagination();


}